//
// Created by hkkzzxz on 24-9-3.
//

#include <vector>
#include <memory>
#include "UserService.h"
#include "MySQLDatabaseModel.h"
#include "UserLogin.h"
#include "Security.h"

std::string UserService::login(const std::string &id, const std::string &password) {
    std::string query = "SELECT salt, password, identity FROM user WHERE id = ?";
    std::vector<std::string> params = {id};
    auto mysqlDatabaseModel = std::make_unique<MySQLDatabaseModel>();
    auto userLogin = mysqlDatabaseModel->queryModel<UserLogin>(query, params);
    if (userLogin.empty()) {
        return "";
    }
    std::string salt = userLogin.front()->getSalt();
    auto security = std::make_unique<Security>();
    std::string hashPassword = security->hashPassword(password, salt);
    if (hashPassword == userLogin.front()->getPassword()) {
        return userLogin.front()->getIdentity();
    }
    return "";
}
